package nii.game.mobileserio.enemy;

import nii.game.mobileserio.interfacesprite.InterfaceSprite;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXLayer;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXProperties;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTileProperty;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTiledMap;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

public class Enemy implements InterfaceSprite{

	// DECLARATIONS =====================================
	int TYPE = 0;

	private BitmapTextureAtlas enemy_BitmapTextureAtlas;
	private TiledTextureRegion enemy_TiledTextureRegion;
	public AnimatedSprite enemy_AnimatedSprite;

	private float positionX = 0;
	private float positionY = 0;
	private boolean is_MoveLeft = true;
	
	//Maps ===========================
	private TMXTiledMap mTMXTiledMap;
	private TMXLayer VatCanTMXLayer;
	private TMXLayer CanEnemyTMXLayer;
	
	//Khai bao Scene =================
	private Scene mScene;

	// CONTRUCTOR ========================================
	public Enemy() {
	};

	//========================================|| onLoadResources ||===========================================//
	
	public void onLoadResources(Engine mEngine, Context mContext) {
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.enemy_BitmapTextureAtlas = new BitmapTextureAtlas(256, 64, TextureOptions.DEFAULT);
		this.enemy_TiledTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.enemy_BitmapTextureAtlas, mContext,
						"turtle.png", 0, 0, 4, 1);
		mEngine.getTextureManager().loadTexture(this.enemy_BitmapTextureAtlas);

	}

	//==========================================|| onLoadScene ||=============================================//
	
	public void onLoadScene(Scene mScene) {

		this.mScene = mScene;
		
		enemy_AnimatedSprite = new AnimatedSprite(this.positionX, this.positionY, this.enemy_TiledTextureRegion.deepCopy());
		enemy_AnimatedSprite.setCullingEnabled(true);
		showEnemyStatus();
		mScene.attachChild(enemy_AnimatedSprite);
 
		
	}

	// moveX ===========================
	public void moveX(float x) {
		this.positionX = x;
		moveEnemy();
	}

	// moveY ===========================
	public void moveY(float y) {
		this.positionY = y;
		moveEnemy();
	}

	// moveEnemy =======================================================
	private void moveEnemy() {
		enemy_AnimatedSprite.setPosition(this.positionX, this.positionY);
	}

	// setStatusEnemy ==================================================
	public void setStatusEnemy(int status) {
		Enemy.this.TYPE = status;
		showEnemyStatus();
	}

	// getStatusEnemy ==================================================
	public int getStatusEnemy() {
		return Enemy.this.TYPE;
	}

	// setPositionXY ===================================================
	public void setPositionXY(float positionX, float positionY) {
		Enemy.this.positionX = positionX;
		Enemy.this.positionY = positionY;
	}

	// showEnemyStatus =================================================
	private void showEnemyStatus() {

		switch (TYPE) {
		
		case 0:// left
		{
			enemy_AnimatedSprite.animate(new long[] { 40, 40 }, 2, 3 , true);
			break;
		}
		case 1:// right
		{
			enemy_AnimatedSprite.animate(new long[] { 40, 40 }, 0, 1 , true);
			break;
		}

		}

	}

	// moveLeft ==========================================================================================================
	public void moveLeft(float pX) {
		if (collidesWith(enemy_AnimatedSprite.getX() - 3, enemy_AnimatedSprite.getY() + enemy_AnimatedSprite.getHeight())
				|| collidesWith_Enemy(enemy_AnimatedSprite.getX() - 3, enemy_AnimatedSprite.getY() + enemy_AnimatedSprite.getHeight())){
			is_MoveLeft = false;
 
		}
		else{
			setStatusEnemy(0);
			enemy_AnimatedSprite.setPosition(enemy_AnimatedSprite.getX() - pX, enemy_AnimatedSprite.getY());
			 
		}
		
	}

	// moveRight ==========================================================================================================
	public void moveRight(float pX) {
		if (collidesWith(enemy_AnimatedSprite.getX() + enemy_AnimatedSprite.getWidth() + 3, enemy_AnimatedSprite.getY() + enemy_AnimatedSprite.getHeight())
				|| collidesWith_Enemy(enemy_AnimatedSprite.getX() + enemy_AnimatedSprite.getWidth() + 3, enemy_AnimatedSprite.getY() + enemy_AnimatedSprite.getHeight())){
			is_MoveLeft = true;
 
		}
		else {
			setStatusEnemy(1);
			enemy_AnimatedSprite.setPosition(enemy_AnimatedSprite.getX() + pX, enemy_AnimatedSprite.getY());
			 
		}
	}

	// moveRandom =========================================================================================================
	public void moveRandom(float pX) {
		 
		
		if (is_MoveLeft == false ){
			moveRight(pX);
			 
		}			
		else if (is_MoveLeft == true ){
			moveLeft(pX);
		} 
		
	}
	
	//Thiet dat map =======================================
	public void setTMXTiledMap(TMXTiledMap mTMXTiledMap){
		this.mTMXTiledMap = mTMXTiledMap;
	}
	
	//Thiet dat vat can ===================================
	public void setTMXLayer(TMXLayer VatCanTMXLayer){
		this.VatCanTMXLayer = VatCanTMXLayer;
	}
	
	//Thiet dat vat can ===================================
	public void setTMXLayer_Enemy(TMXLayer _canEnemyTMXLayer){
		 CanEnemyTMXLayer = _canEnemyTMXLayer;
	}
		
	//Kiem tra va cham =================================================================================
	public boolean collidesWith(float pX, float pY){
		TMXTile mTMXTile = VatCanTMXLayer.getTMXTileAt(pX,pY);
    	try{
			if(mTMXTile == null){
				System.out.println("mTMXTile = null");
			}
			else{
				TMXProperties<TMXTileProperty> mTMXProperties= mTMXTile.getTMXTileProperties(mTMXTiledMap);
				TMXTileProperty mTMXTileProperty = mTMXProperties.get(0);
				if(mTMXTileProperty.getName().equals("vatcan")){
				}
			}
			
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	//Kiem tra va cham ===================================================================================
	public boolean collidesWith_Enemy(float pX, float pY){
		TMXTile mTMXTile = CanEnemyTMXLayer.getTMXTileAt(pX,pY);
    	try{
			if(mTMXTile == null){
				System.out.println("mTMXTile = null");
			}
			else{
				TMXProperties<TMXTileProperty> mTMXProperties= mTMXTile.getTMXTileProperties(mTMXTiledMap);
				TMXTileProperty mTMXTileProperty = mTMXProperties.get(0);
				if(mTMXTileProperty.getName().equals("can_enemy")){
				}
			}
			
			return true;
		}catch(Exception e){
			return false;
		}
	}
 
	// deleteEnemy =========================================================================================
	public void deleteEnemy(){
		mScene.detachChild(enemy_AnimatedSprite);
	}
}
